home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / demos / OpenGL / backtrace / Color.h < prev    next >
C/C++ Source or Header  |  1996-11-11  |  5KB  |  190 lines

  1. /*
  2.  * (c) Copyright 1993, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED 
  4.  * Permission to use, copy, modify, and distribute this software for 
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that 
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission. 
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  * 
  25.  * US Government Users Restricted Rights 
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36.  */
  37. #ifndef COLOR_H
  38. #define COLOR_H
  39.  
  40. class Color {
  41.  public:
  42.   inline Color();
  43.   inline Color(GLfloat r, GLfloat g, GLfloat b, GLfloat a = 1.0);
  44.   
  45.   inline Color& operator=(GLfloat *a);
  46.   inline Color& operator=(GLfloat a);
  47.   inline Color& operator+(Color a);
  48.   inline Color& operator+=(Color a);
  49.   inline Color& operator*(Color a);
  50.   inline Color& operator*(GLfloat a);
  51.   inline Color& operator*=(Color a);
  52.   inline Color& operator*=(GLfloat *a);
  53.   inline Color& operator*=(GLfloat a);
  54.  
  55.   inline GLfloat& operator[](int index);
  56.  
  57.   inline Color clamp();
  58.   
  59.   inline void glcolor();
  60.  
  61.   inline void print();
  62.   inline void print(const char *format);
  63.  
  64.   GLfloat c[4];
  65. };
  66.  
  67. inline Color::Color()
  68. {
  69. }
  70.  
  71. inline Color::Color(GLfloat r, GLfloat g, GLfloat b, GLfloat a)
  72. {
  73.   c[0] = r;
  74.   c[1] = g;
  75.   c[2] = b;
  76.   c[3] = a;
  77. }
  78.  
  79. inline Color& Color::operator=(GLfloat a)
  80. {
  81.   c[0] = c[1] = c[2] = c[3] = a;
  82.   return *this;
  83. }
  84.  
  85. inline Color& Color::operator=(GLfloat *a)
  86. {
  87.   c[0] = a[0];
  88.   c[1] = a[1];
  89.   c[2] = a[2];
  90.   c[3] = a[3];
  91.   return *this;
  92. }
  93.  
  94. inline Color& Color::operator+(Color a) 
  95. {
  96.   Color val;
  97.   val.c[0] = c[0] + a.c[0];
  98.   val.c[1] = c[1] + a.c[1];
  99.   val.c[2] = c[2] + a.c[2];
  100.   val.c[3] = c[3] + a.c[3];
  101.   return val;
  102. }
  103.  
  104. inline Color& Color::operator+=(Color a)
  105. {
  106.   c[0] += a.c[0];
  107.   c[1] += a.c[1];
  108.   c[2] += a.c[2];
  109.   c[3] += a.c[3];
  110.   return *this;
  111. }
  112.  
  113. inline Color& Color::operator*(Color a)
  114. {
  115.   Color val;
  116.   val.c[0] = c[0] * a.c[0];
  117.   val.c[1] = c[1] * a.c[1];
  118.   val.c[2] = c[2] * a.c[2];
  119.   val.c[3] = c[3] * a.c[3];
  120.   return val;
  121. }
  122.  
  123. inline Color& Color::operator*(GLfloat a)
  124. {
  125.   Color val;
  126.   val.c[0] = c[0] * a;
  127.   val.c[1] = c[1] * a;
  128.   val.c[2] = c[2] * a;
  129.   val.c[3] = c[3] * a;
  130.   return val;
  131. }
  132.  
  133. inline Color& Color::operator*=(Color a)
  134. {
  135.   c[0] *= a.c[0];
  136.   c[1] *= a.c[1];
  137.   c[2] *= a.c[2];
  138.   return *this;
  139. }
  140.  
  141. inline Color& Color::operator*=(GLfloat *a) 
  142. {
  143.   c[0] *= a[0];
  144.   c[1] *= a[1];
  145.   c[2] *= a[2];
  146.   return *this;
  147. }
  148.  
  149. inline Color& Color::operator*=(GLfloat a)
  150. {
  151.   c[0] *= a;
  152.   c[1] *= a;
  153.   c[2] *= a;
  154.   c[3] *= a;
  155.   
  156.   return *this;
  157. }
  158.  
  159. inline GLfloat& Color::operator[](int index)
  160. {
  161.   return c[index];
  162. }
  163.  
  164. inline Color Color::clamp()
  165. {
  166.   Color val;
  167.   val.c[0] = c[0] < 0.0 ? 0.0 : (c[0] > 1.0 ? 1.0 : c[0]);
  168.   val.c[1] = c[1] < 0.0 ? 0.0 : (c[1] > 1.0 ? 1.0 : c[1]);
  169.   val.c[2] = c[2] < 0.0 ? 0.0 : (c[2] > 1.0 ? 1.0 : c[2]);
  170.   val.c[3] = c[3] < 0.0 ? 0.0 : (c[3] > 1.0 ? 1.0 : c[3]);
  171.   return val;
  172. }
  173.  
  174. inline void Color::glcolor()
  175. {
  176.   glColor4fv(c);
  177. }
  178.  
  179. inline void Color::print()
  180. {
  181.   print("%f %f %f %f\n");
  182. }
  183.  
  184. inline void Color::print(const char *format)
  185. {
  186.   printf(format, c[0], c[1], c[2], c[3]);
  187. }
  188.  
  189. #endif
  190.